package com.yuexunit.operationman.nbsy.module.plant.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.yuexunit.operationman.nbsy.R;
import com.yuexunit.operationman.nbsy.module.plant.base.ICallBack;
import com.yuexunit.operationman.nbsy.module.plant.bean.ImplantMaterialItemListEntity;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by chenss on 2017/7/11 0011.
 */

public class PlantAdapter extends BaseAdapter {

    List<ImplantMaterialItemListEntity> materialItemList = new ArrayList<>();

    protected LayoutInflater mInflater;
    Context context;
    ICallBack iCallBack;
    boolean change;

    public PlantAdapter(Context context, List<ImplantMaterialItemListEntity> materialItemList,
                        ICallBack iCallBack, boolean change) {
        this.materialItemList = materialItemList;
        this.context = context;
        this.iCallBack = iCallBack;
        this.change = change;
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public void setMaterialItemList(List<ImplantMaterialItemListEntity> materialItemList) {
        this.materialItemList = materialItemList;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return materialItemList == null ? 0 : materialItemList.size();
    }

    @Override
    public Object getItem(int i) {
        return materialItemList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if (view == null) {
            view = this.mInflater.inflate(R.layout.item_plant_content, viewGroup, false);
            holder = new ViewHolder(view);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        holder.position = i;
        holder.num.setText((i + 1) + "");
        holder.number.setText("编号：" + materialItemList.get(i).getNumber());
        holder.name.setText("名称：" + materialItemList.get(i).getProductName());
        holder.standard.setText("规格：" + materialItemList.get(i).getStandard());
        holder.model.setText("型号：" + materialItemList.get(i).getModel());
        holder.producer.setText("生产商：" + materialItemList.get(i).getManufacturer());
        holder.provider.setText("供应商：" + materialItemList.get(i).getSupplierName());
        holder.batchNumber.setText("批号：" + materialItemList.get(i).getBatchNumber());
        holder.validity.setText("有效期：" + materialItemList.get(i).getValidityPeriod());
        holder.registration.setText("注册证：" + materialItemList.get(i).getRegistration());
        holder.quantity.setText(materialItemList.get(i).getAmount());
        float price = Float.valueOf(materialItemList.get(i).getPrice());
        holder.price.setText("¥" + Integer.valueOf(materialItemList.get(i).getAmount()) * price);

        if(change){
            holder.subtract.setVisibility(View.VISIBLE);
            holder.add.setVisibility(View.VISIBLE);
        }else{
            holder.subtract.setVisibility(View.GONE);
            holder.add.setVisibility(View.GONE);
        }
        return view;
    }

    class ViewHolder {
        @Bind(R.id.num)
        TextView num;
        @Bind(R.id.number)
        TextView number;
        @Bind(R.id.name)
        TextView name;
        @Bind(R.id.standard)
        TextView standard;
        @Bind(R.id.model)
        TextView model;
        @Bind(R.id.producer)
        TextView producer;
        @Bind(R.id.provider)
        TextView provider;
        @Bind(R.id.batch_number)
        TextView batchNumber;
        @Bind(R.id.quantity)
        TextView quantity;
        @Bind(R.id.price)
        TextView price;
        @Bind(R.id.validity)
        TextView validity;
        @Bind(R.id.registration)
        TextView registration;

        @Bind(R.id.subtract)
        ImageView subtract;
        @Bind(R.id.add)
        ImageView add;

        int position;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);

            subtract.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(materialItemList.get(position).getAmount().equals("1")){
                        materialItemList.remove(position);
                    }else{
                        int amount = Integer.valueOf(materialItemList.get(position).getAmount()) - 1;
                        materialItemList.get(position).setAmount(amount + "");
                    }
                    notifyDataSetChanged();
                    if(iCallBack != null){
                        iCallBack.callBack();
                    }
                }
            });

            add.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int amount = Integer.valueOf(materialItemList.get(position).getAmount()) + 1;
                    materialItemList.get(position).setAmount(amount + "");
                    notifyDataSetChanged();
                    if(iCallBack != null){
                        iCallBack.callBack();
                    }
                }
            });
        }
    }
}
